package me.yokeyword.fragmentation.debug;

import me.yokeyword.fragmentation.ISupportAbility;
import me.yokeyword.fragmentation.ResourceTable;
import me.yokeyword.fragmentation.extensions.FractionEx;
import me.yokeyword.fragmentation.util.ResUtil;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.media.image.PixelMap;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class DebugStackDelegate {

    FractionAbility mFractionAbility;

    public DebugStackDelegate(FractionAbility fractionAbility) {
        this.mFractionAbility = fractionAbility;
    }

    public void onPostCreate(FractionAbility rootId, int stackLayout) {
        Component root = rootId.findComponentById(stackLayout);
        if (root instanceof StackLayout) {
            StackLayout content = (StackLayout) root;
            final Image stackView = new Image(mFractionAbility);
            Optional<PixelMap> pixelMap = ResUtil.getPixelMap(mFractionAbility, ResourceTable.Media_fragmentation_ic_stack);
            if (pixelMap.isPresent())
                stackView.setPixelMap(pixelMap.get());
            StackLayout.LayoutConfig textParam = new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT, LayoutAlignment.END);
            stackView.setLayoutConfig(textParam);
            stackView.setMarginTop(50);
            stackView.setMarginRight(20);
            content.addComponent(stackView);
            stackView.setClickedListener(component -> {
                List<FractionEx> activeFractions = ((ISupportAbility) mFractionAbility).getSupportDelegate().getSupportFractionManagerEx().getActiveFractions();
                String str[] = new String[activeFractions.size()];
                int count = 0;
                for (FractionEx fractionEx : activeFractions) {
                    fractionEx.getFraction().getClass().getSimpleName();
                    if ((count + 1) == activeFractions.size()) {
                        str[count] = fractionEx.getFraction().getClass().getSimpleName() + "  ☀";
                    } else {
                        str[count] = fractionEx.getFraction().getClass().getSimpleName();
                    }
                    count++;
                }

                new CustomDialog.Builder(mFractionAbility)
                        .icon(ResourceTable.Media_fragmentation_help)
                        .title(ResourceTable.String_t_Stack)
                        .items(reverseArray(str))
                        .positiveText(ResourceTable.String_t_cancel)
                        .show();

            });

        }
    }

    private String[] reverseArray(String[] arr) {
        List<String> list = Arrays.asList(arr);
        Collections.reverse(list);
        String[] reversedArray = list.toArray(arr);
        return reversedArray;
    }
}
